package com.wn.project.web;

import com.wn.common.PageBean;
import com.wn.common.ResponseEntity;
import com.wn.entity.Document;
import com.wn.project.service.DocumentService;
import com.wn.utils.OssManagerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @program: pms
 * @ClassName DocumentController
 * @description:
 * @author: LRW
 * @create: 2022-10-25 11:04
 * @Version 1.0
 **/
@RestController
@RequestMapping("/doc")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    /***
     * @description: 新增文档
     * @param: document
     * @return: com.wn.common.ResponseEntity
     * @author Lrw
     * @date: 2022/10/26 17:34
     */
    @PostMapping("/add")
    public ResponseEntity add(@RequestBody Document document){
        documentService.add(document);
        return ResponseEntity.success;
    }

    /***
     * @description: 根据id删除文档
     * @param: id
     * @return: com.wn.common.ResponseEntity
     * @author Lrw
     * @date: 2022/10/26 19:07
     */
    @RequestMapping("/delete/{id}")
    public ResponseEntity delete(@PathVariable int id){
        documentService.deleteById(id);
        return ResponseEntity.success;
    }

    /***
     * @description: 分页多条件查询
     * @param: document条件 pageSize分页量 currPage当前页码
     * @return: com.wn.common.ResponseEntity
     * @author Lrw
     * @date: 2022/10/26 17:33
     */
    @RequestMapping("/list/{pageSize}/{currPage}")
    public ResponseEntity getList(@RequestBody Document document,
                                  @PathVariable int pageSize,
                                  @PathVariable int currPage){
        PageBean<Document> p = documentService.getByCondition(document, pageSize, currPage);
        return new ResponseEntity("200","ok",p);
    }

    /***
     * @description: 修改文档
     * @param: document
     * @return: com.wn.common.ResponseEntity
     * @author Lrw
     * @date: 2022/10/26 17:32
     */
    @PostMapping("/update")
    public ResponseEntity update(@RequestBody Document document){
        documentService.update(document);
        return ResponseEntity.success;
    }

    /***
     * @description: 根据id查文档
     * @param: id
     * @return: com.wn.common.ResponseEntity
     * @author Lrw
     * @date: 2022/10/26 19:05
     */
    @GetMapping("/get/{id}")
    public ResponseEntity getById(@PathVariable int id){
        Document document = documentService.getById(id);
        return new ResponseEntity("200","ok",document);
    }
    /***
     * @description: 文件上传
     * @param: file
     * @return: com.wn.common.ResponseEntity 文件路径
     * @author Lrw
     * @date: 2022/10/19 15:27
     */
    @RequestMapping("/upload")
    public ResponseEntity upload(MultipartFile file) {
        String url = null;
        try {
            String filename = file.getOriginalFilename();
            filename= UUID.randomUUID().toString().replace("-","")+"."+filename;
            url = OssManagerUtil.uploadImage(filename,file.getInputStream().available(),file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("400","fail","文件上传失败");
        }

        return new ResponseEntity(url);
    }

}
